﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Phoenix.Service.Contracts;
using Phoenix.Service;
using Phoenix.Service.Model;
namespace Phoenix.AdminSite.Controllers
{
	public class RightsController : ControllerBase
    {
		IRightsService IRights = new RightsService();
        IIDCreaterService IIDCreater = new IDCreaterService();

        public ActionResult Index()
        {
			int typeID = 0;
			if (!string.IsNullOrEmpty(Request.QueryString["typeid"]))
			{
				typeID = int.Parse(Request.QueryString["typeid"]);

				ViewData["RightsList"] = IRights.GetByRightsTypeID(typeID);
                ViewData["RightTypeID"] = typeID;
			}

			return View();

        }

		public ActionResult Edit()
		{
			int rightID = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["rightID"]))
			{
                rightID = int.Parse(Request.QueryString["rightID"]);

				ViewData["RightsModel"] = IRights.GetByRightID(rightID);
			}

            ViewData["RightsTypeID"] = Request.QueryString["rightTypeID"];

			return View();
		}

		public ActionResult Save(FormCollection collection)
		{

			if (!string.IsNullOrEmpty(collection["hdRightID"]))
			{
                RightsModel model = IRights.GetByRightID(int.Parse(collection["hdRightID"]));
                model.RightName = collection["txtRightName"];
                model.RightDisplayName = collection["txtRightDisplayName"];
			    model.RightMemo = collection["txtRightMemo"];
                model.UpdateUser = 0;
				model.UpdateTime = DateTime.Now;
				IRights.UpdateByRightID(model);
			}
			else
			{
                RightsModel model = new RightsModel();
                model.RightID = IIDCreater.GetNewID("RightID", "权限编号", 1000);
                model.RightName = collection["txtRightName"];
                model.RightTypeID = int.Parse(collection["hdRightTypeID"]);
                model.SystemFlag = 1;
                model.RightDisplayName = collection["txtRightDisplayName"];
                model.RightMemo = collection["txtRightMemo"];
                model.CreateUser = 1;
                model.CreateTime = DateTime.Now;
                model.UpdateTime = DateTime.MinValue;
				IRights.AddRights(model);

			}

			return ShowMsg("成功", Common.ActionStatus.OK, "");
		}

        public ActionResult Delete(int rightID)
        {
            IRights.DeleteByRightID(rightID);
            return ShowMsg("成功", Common.ActionStatus.OK, "");
        }

    }
}
